package com.lanyam.travel.base.entity.base;

import com.lanyam.travel.base.exception.*;
import com.lanyam.travel.base.utils.StateUtils;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

/**
 * @author KingTang
 * @date   2019/12/20
 */
public class ErrorResponseBuilder {
    private Exception exception;
    public ErrorResponseBuilder exception(Exception exception)
    {
        this.exception = exception;
        return this;
    }

    public BaseResponse build()
    {
        if(exception == null)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_SYS_ERROR);
        }
        else if(exception instanceof UnloginException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_UNLOGIN);
        }
        else if(exception instanceof PermissionDeniedException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_PERMISSION_DENIED);
        }
        else if(exception instanceof SystemErrorException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_SYS_ERROR);
        }
        else if(exception instanceof LoseParametersException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_LOSS_PARAMETERS);
        }
        else if(exception instanceof PasswordErrorException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_PASSWORD_ERROR);
        }
        else if(exception instanceof ErrorParametersException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_PARAMETERS_ERROR);
        }
        else if(exception instanceof MethodArgumentTypeMismatchException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_PARAMETERS_ERROR);
        }
        else if(exception instanceof IllegalStateException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_PARAMETERS_ERROR);
        }
        else if (exception instanceof VerifycodeFailedException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_VERIFYCODE_FAILED);
        }
        else if (exception instanceof VerifycodeTooManyTimesException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_VERIFYCODE_TOO_MANY_TIMES);
        }
        else if (exception instanceof ApplyVerifyCodeTooOften)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_VERIFYCODE_TOO_OFEN);
        }
        else if (exception instanceof DataIntegrityViolationException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_PARAMETERS_ERROR);
        }
        else if (exception instanceof MissingServletRequestParameterException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_LOSS_PARAMETERS);
        }
        else if (exception instanceof org.springframework.validation.BindException)
        {
            return BaseResponse.getInstance(StateUtils.ERRCODE_LOSS_PARAMETERS);
        }
        else {
            return BaseResponse.getInstance(StateUtils.ERRCODE_SYS_ERROR);
        }
    }

    @Override
    public String toString() {
        int state = build().getErrcode();
        return "{\"errcode\":" + state + "}";
    }
}
